//
//  AddIdentityViewController.m
//  Kfangchan
//
//  Created by 朋飞 on 14-11-13.
//  Copyright (c) 2014年 Beijing Renjuren Network Technology Co., Ltd. All rights reserved.
//

#import "AddIdentityViewController.h"
#pragma mark 添加身份证号
#pragma mark
@interface AddIdentityViewController ()
@property (strong, nonatomic) IBOutlet UIButton *saveBtn;

@property (strong, nonatomic) IBOutlet UITextField * identityTextField;
@end

@implementation AddIdentityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 返回
    self.navigationItem.titleView = [[Theam currentTheam] navigationTitleViewWithTitle:@"添加身份证号"];
    self.navigationItem.leftBarButtonItem = [[Theam currentTheam] navigationBarLeftButtonItemWithImage:[UIImage imageNamed:@"nav_back.png"] Target:self Selector:@selector(back)];
    
    self.navigationItem.rightBarButtonItem = [[Theam currentTheam] navigationBarRightButtonItemWithImage:nil Title:@"保存" Target:self Selector:@selector(save)];
    
    
    
}

#pragma mark  保存
#pragma mark
-(void)save{
    BOOL  verify = [self verify];
    if(verify){
        //弱引用
        __weak AddIdentityViewController *weak_self = self;
        // 构建请求参数
        NSDictionary *param = @{
                                @"idcard":self.identityTextField.text
                                
                                };
        // 请求
        MKNetworkOperation *operation = [[BaseEngine sharedEngine]RunRequest:[param mutableCopy] path:KFC_UPDATE completionHandler:^(id responseObject) {
            
            
            if(responseObject){
                
              //  NSDictionary* dic = [AppConfig getAgent];
                NSMutableDictionary* dic = [[AppConfig getAgent] mutableCopy];
                NSString *str = [NSString stringWithString:self.identityTextField.text];
                [dic setValue: str forKey:@"idcard"];
                [AppConfig setAgent:dic];
                [weak_self back];
            }
            
            
        } errorHandler:^(NSError *error){
            
            showCustomAlertMessage(@"网路错误");
            
        }finishHandler:^(id responseObject) {
            
            
        }];
        
        [self.opHandlers addObject:operation];
        
    }
}

#pragma mark -
#pragma mark 关闭键盘
- (IBAction)hideKeyboard {
    [self.identityTextField resignFirstResponder];
}

-(BOOL) verify{
    NSString *identity = self.identityTextField.text;
    
    UIAlertView *alert = nil;
    
    
    if(identity.length==0){
        alert = [[UIAlertView alloc]initWithTitle:nil message:@"请输入身份证号" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return NO;
    }
    
    if(!validateIdentityCard(identity)){
        alert = [[UIAlertView alloc]initWithTitle:nil message:@"请输入正确的身份证号" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return NO;
    }

    
    //    if(identity.length<6){
    //        alert = [[UIAlertView alloc]initWithTitle:nil message:@"用户名不少于6个字符" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    //        [alert show];
    //        return NO;
    //    }
    //
    
    
    
    
    return YES ;
}



#pragma mark 返回
#pragma mark
- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
